/*
* @Author: sc
* @Date:   2019-01-10 11:34:15
* @Last Modified by:   sc
* @Last Modified time: 2019-01-23 11:39:17
*/
import { observable, computed, action, configure } from 'mobx';

import Service from 'service/index.jsx';
const _service = new Service();

import Util   from 'util/index.jsx';
const _util = new Util();

import Todo from 'store/todo/index.jsx';

configure({
    enforceActions : "always"
});

class TodoList{
	@observable firstLoading = true;
	// ID
	@observable id = 1;
	@observable todos = {};
	@observable todoIDs = [];
	

	// 搜索标志
	@observable searchKey = true;

	// 分页相关，均为默认值
	@observable pageNumber = 1;
	@observable pageSize = 5;
	@observable offset = 5;
	@observable limit = 1;

	// 标签
	@observable total_tips = 1;
	@observable finish_tips = 0;
	@observable left_tips = 0;

	@observable searchInfo = '';

	// 获取所有的计划列表
	@action getThing(){
		// this.searchKey = false;
		let query = {
			offset : this.offset,
			limit : this.limit,
			info : this.searchInfo || ''
		};
		_service.getList(query).then(res => {
			this.init(res.result);
		}, errMsg => {
			_util.errTips(errMsg);
		});
	}

	@action init(info){
		this.setTips(info.total, info.finished, info.left);
		this.todos = {};
		this.todoIDs = [];
		if(info.data.length > 0){
			info.data.forEach(item => {
			this.todos[item.id] = new Todo(item);
			this.todoIDs.push(item.id);
			});
			this.id = this.todoIDs[this.todoIDs.length - 1]
		}
	}
	@action setTips(total, finished, left){
		this.total_tips = total;
		this.finish_tips = finished;
		this.left_tips = left;
	}
	// 变更firstLoading
	@action changeFirstLoading = () => {
		this.firstLoading = !this.firstLoading;
	}
	// 增加计划
	@action addThing(info){
		let query = {
			info : info
		};
		_service.addThing(query).then(action((res) => {
			if(res.status.code === 1){
				this.setTips(res.result.total, res.result.finished, res.result.left);
				// 回到第一页，并刷新
				this.setPageInfo(1, this.pageSize);
			}else{
				_util.errTips(res.status.message);
			}
		}), errMsg => {
			_util.errTips(errMsg);
		});
	}
	// 完成计划
	@action finishThing(id){
		let query = {
			id : id,
		}
		_service.finished(query).then(res => {
				if(res.status.code === 1){
					this.setTips(res.result.total, res.result.finished, res.result.left);
					this.getThing();
				}else{
					_util.errTips(res.status.message);
				}
			}, errMsg => {
				_util.errTips(errMsg);
			});
	}
	@action deleteThing(id){
		let query = {
			id : id,
		}
		_service.deleteById(query).then(res => {
			if(res.status.code === 1){
				this.setTips(res.result.total, res.result.finished, res.result.left);
				// 当一页删除完时，跳转到前一页
				if((this.total_tips % this.pageSize === 0) && (this.pageNumber - 1 === (this.total_tips / this.pageSize))){
					if(this.pageNumber - 1 !==0){
						this.setPageInfo(this.pageNumber - 1,this.pageSize);
					}
				}else{
					this.getThing();
				}
			}
			else{
				_util.errTips(res.status.message);
			}
		}, errMsg => {
			_util.errTips(errMsg);
		});
	}
	@action updateThing(id, info){
		let query = {
			id : id,
			info: info
		}
		_service.updateById(query).then(res => {
				if(res.status.code === 1){
					this.getThing();
				}else{
					_util.errTips(res.status.message);
				}
			}, errMsg => {
				_util.errTips(errMsg);
			});
	}
	
	@action setSearchInfo(value){
		this.searchInfo = value;
		this.pageNumber = 1;
		this.setPageInfo(1, this.pageSize);
 	}
	// 分页回调设置offset、limit、pageNum
	@action setPageInfo(page, pageSize){
		this.pageNumber = page;
		this.pageSize = pageSize;
		// 第一页
		if(this.total_tips < this.pageSize || this.pageNumber <= 1){
			this.offset = this.pageSize;
			this.limit = 1;
		}
		else if((this.pageNumber * this.pageSize) > this.total_tips){
			this.offset = this.total_tips -((this.pageNumber - 1) * this.pageSize) ;
			this.limit = ((this.pageNumber - 1) * this.pageSize) + 1;
		}
		else{
			this.offset = this.pageSize;
			this.limit = ((this.pageNumber - 1) * this.pageSize) + 1;
		}
		this.getThing();
	}

}

export default new TodoList();